package com.dzsw.mybatisx.gen;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.dzsw.mybatisx.gen.util.GenConf;
import com.dzsw.mybatisx.gen.util.MyFreemarkerEngine;
import com.dzsw.mybatisx.gen.util.MyMysqlTypeConvert;
import com.dzsw.utils.YamlUtil;

/**
 * @author sunz
 * @version 1.0
 * @CreateTime 2021-04-21-11:24
 */
public class OracleGenerator {

    public static void main(String[] args) throws Exception {
        GenConf conf = YamlUtil.readGenConfYaml("oracle.yml");
        String[] tableNames = new String[]{
                "ATTENTION",
                "ARTICLE",
        };
        generate(conf, tableNames);
    }

    public static void generate(GenConf conf, String... tableNames) {
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(conf.getOutputPath());
        globalConfig.setAuthor(conf.getAuthor());
        globalConfig.setEnableCache(false);// XML 二级缓存
        globalConfig.setBaseResultMap(true);// XML ResultMap
        globalConfig.setBaseColumnList(true);// XML columnList
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(true);
        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        globalConfig.setMapperName("%s" + conf.getMapperName());
        globalConfig.setXmlName("%s" + conf.getXmlName());
        globalConfig.setServiceName("I%s" + conf.getServiceName());
        globalConfig.setServiceImplName("%s" + conf.getServiceImplName());
        globalConfig.setControllerName("%s" + conf.getControllerName());
        mpg.setGlobalConfig(globalConfig);
        // 数据源
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.ORACLE);
        dsConfig.setTypeConvert(new MyMysqlTypeConvert());
        // 配置数据源
        dsConfig.setUrl(conf.getUrl());
        dsConfig.setDriverName(conf.getDriverName());
        dsConfig.setUsername(conf.getUsername());
        dsConfig.setPassword(conf.getPwd());
        dsConfig.setSchemaName(conf.getSchemeName());
        mpg.setDataSource(dsConfig);
        // 包配置
        PackageConfig pkgConf = new PackageConfig();
        pkgConf.setModuleName(conf.getModuleName());
        pkgConf.setParent(conf.getPkgParent());
        mpg.setPackageInfo(pkgConf);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperControllerClass(conf.superControllerClass);
//        strategy.setSuperEntityClass(conf.superEntityClass);
        strategy.setSuperMapperClass(conf.getSuperMapperClass());
        strategy.setRestControllerStyle(true);
        strategy.setEntityLombokModel(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setInclude(tableNames);
        // 模板配置
//        TemplateConfig templateConf = new TemplateConfig();
//        templateConf.setEntity("templates/entity.java.ftl");
//        templateConf.setMapper("templates/mapper.java.ftl");
//        templateConf.setXml("templates/mapper.xml.ftl");
//        templateConf.setServiceImpl("templates/service.java.ftl");
//        templateConf.setController("templates/controller.java.ftl");
        // 配置生成策略和模板引擎
        mpg.setStrategy(strategy);
//        mpg.setTemplateEngine(new MyTemplateEngine());
        mpg.setTemplateEngine(new MyFreemarkerEngine());
        // 生成
        mpg.execute();
    }
}
